package com.cnpinxi.controller.web.product;

import com.cnpinxi.controller.base.BaseController;
import com.cnpinxi.def.Constant;
import com.cnpinxi.domain.*;
import com.cnpinxi.myRunTime.MyRunTimeExcption;
import com.cnpinxi.result.ResultObj;
import com.cnpinxi.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lutao on 2017/7/14.
 */
@RequestMapping("/product")
@Controller
public class ProductController extends BaseController {
    @Autowired
    ProductService productService;
    @Autowired
    ProductEntityService productEntityService;
    @Autowired
    ProductTypeService productTypeService;
    @Autowired
    PackageRelationService packageRelationService;
  @Autowired
  UserService userService;  @Autowired
  OrderService orderService;

    /**
     * 查询所有产品名称
     *
     * @param request
     * @return
     */
    @RequestMapping("/getProductList")
    public ModelAndView getProductList(HttpServletRequest request, Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/admin/product/getProductList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        List<Product> productList = productService.getProductList(map);
        PageInfo<Product> pageinfo = new PageInfo<>(productList);
        m.addObject("pageInfo", pageinfo);
        m.addObject("bean", map);
        return m;
    }
/**
     * 查询所有产品名称前端显示
     *
     * @param request
     * @return
     */
    @RequestMapping("/productList")
    public ModelAndView productList(HttpServletRequest request, Integer page, Integer pageSize) {
        ModelAndView m = new ModelAndView("/wap/product/productList");
        Map<String, String> map = this.showRequestParameters(request);
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        List<Product> productList = productService.getProductList(map);
        PageInfo<Product> pageinfo = new PageInfo<>(productList);
        m.addObject("pageInfo", pageinfo);
        m.addObject("bean", map);




        return m;
    }


    /**
     * 跳转到产品名称页面
     *
     * @param productId
     * @return
     */

    @RequestMapping("{productId}")
    public ModelAndView product(HttpServletRequest request, HttpServletResponse response, @PathVariable("productId") Long productId) {
        ModelAndView m = new ModelAndView();
        m.setViewName("/web/product/product");
        HttpSession session = request.getSession();
        if (super.getIsMobile(request)) {
            m.setViewName("/wap/product/product");

            super.oauth2(request, response);
            String openId = (String) session.getAttribute(Constant.SESSION_OPEN_ID);

            try {
                User user = super.getUserByOpenId(openId);
                if(user!=null){
                    session.setAttribute(Constant.SESSION_KEY_USER_LOGIN, user);
                    if(StringUtils.isNotBlank(openId)){
                        BindDistributor bindDistributor=userService.getBindDistributor(openId);
                        m.addObject("activityId", bindDistributor.getParentId());
                    }

                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }


        m.addObject("productId", productId);


        return m;
    }

    /**
     * 编辑产品名称
     *
     * @param productId
     * @return
     */
    @RequestMapping(value = "/getProduct")
    @ResponseBody
    public String getProduct(Long productId) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            if (productId == null) {
                throw new MyRunTimeExcption("产品不存在！");
            }
            Map<String, String> map = new HashMap<>();
            map.put("productId", String.valueOf(productId));
          logger.info("获取产品............................");
            Product product = productService.getProduct(map);
            resultObj.setData(product);
            resultObj.setMsg("操作成功！");
            resultObj.setCode(ResultObj.CODE_SUCCESS);

        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    /**
     * 编辑产品名称
     *
     * @param productEntityId
     * @return
     */
    @RequestMapping(value = "/getProductPackage")
    @ResponseBody
    public String getProductPackage(Long productEntityId) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            if (productEntityId == null) {
                throw new MyRunTimeExcption("产品不存在！");
            }
            Map<String, String> map = new HashMap<>();
            map.put("productEntityId", String.valueOf(productEntityId));
            List<PackageRelation> packageRelationList = packageRelationService.getPackageRelationList(map);
            resultObj.setData(packageRelationList);
            resultObj.setMsg("操作成功！");
            resultObj.setCode(ResultObj.CODE_SUCCESS);

        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }


    /**
     * 编辑产品名称
     *
     * @param product
     * @return
     */
    @RequestMapping(value = "/getProductSubmit")
    @ResponseBody
    public String getProductSubmit(Product product) {
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            productService.modifyProduct(product);
            resultObj.setMsg("操作成功！");
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    /**
     * 按名称查询该产品
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getProductByName")
    @ResponseBody
    public String getProductByName(HttpServletRequest request) {
        Map<String, String> map = this.showRequestParameters(request);
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            List<Product> products = productService.getProductList(map);
            resultObj.setData(products);
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            e.printStackTrace();
        }
        return resultObj.toJson();
    }


    /**
     * 按名称查机构
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getProductEntityByName")
    @ResponseBody
    public String getProductEntityByName(HttpServletRequest request) {
        Map<String, String> map = this.showRequestParameters(request);
        ResultObj resultObj = new ResultObj();
        resultObj.setCode(ResultObj.CODE_SUCCESS);
        try {
            List<ProductEntity> productEntityList = productEntityService.getProductEntityList(map);
            resultObj.setData(productEntityList);
        } catch (Exception e) {
            resultObj.setCode(ResultObj.CODE_FAIL);
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
}
